
var imManager = {

 	windows: {},
 	html: '',
 	
 	/**
    * Create new IM window
    *
    * @author G Henson
    **/
   create: function(myuser, dest) {
   
   	  // id of the window to create
      var winId = dest.getId() + winidSuffix;
	  
	  // creation of the window
	  this.windows[dest.getId()] = new Moossenger(  winId, myuser, dest, {
													left: 600,
													top: 10,
													width: 300,
													height: 400,
													title: dest.getName(),
													//container: container1,
													resizeLimit: {'x':[250], 'y':[200]},
													theme: this.getTheme (windooStyle),
													position: false,
													buttons: {menu: true},
													onFocus: function() {
														this.notify();
													},
													onRestore: function() {
														this.notify();
													},
													onResizeComplete: function () {
														this.resizeDialogBox();
													},
													onClose: function () {
														this.sendCloseEvent();
														System.toggleChatList(this.recipient);
													}
													});
		
		//$html = this.getHTML(this.windows[toid], toid);
		//this.windows[toid].setHTML($html).show();
		
		// ajout de la toolbar
		// TODO A modifier
		var htmltoolbar = this.windows[dest.getId()].getToolbar().buildHTMLToolbar(dest.getId());
		var toolb = new Element ('div', {'id' : winId+toolbarid, 'style' : 'display:none;'});
		toolb.setHTML(htmltoolbar);
		$E('body').adopt(toolb);
				
		var myhtml = this.html.replace(/%winid%/g, winId).replace (/%userid%/g, dest.getId()).replace (/%path%/g, pathTous);
		
		// ajoute la toolbar a la fenetre
		this.windows[dest.getId()].addPanel($(winId+toolbarid), 'bottom');
		// rend la toolbar visible
		toolb.setStyle('display', 'block');
		
		this.windows[dest.getId()].setHTML(myhtml).show();
		this.windows[dest.getId()].resizeDialogBox();

		//** move the window on top
		this.windows[dest.getId()].bringTop();   
		
		/** add the user to the list of current chats
		*/
		System.toggleChatList(dest);
		
		/** TODO
		**/
		Windoo.$wm.register(this.windows[dest.getId()]);  
		Windoo.$wm.focus(this.windows[dest.getId()]);
   }, 
	
	/**
	 * get the content of the template html file to be displayed in a window
	 * the html attibute of im.Manager is filled with the content found.
	 * @author : g. henson 
	*/
	getHTMLtpl: function () {
		var tplfile = pathTous + 'html/template_'+winstyle+'.html';
		var myXhr = new XHR({method: 'get', 
							 onRequest: function () {
							 	$(moosmainid).setStyle('display', 'none');
							 	new Element('div').setProperty('id', loadingid).injectInside(document.body);
								$(loadingid).innerHTML = "<img src='"+pathTous+"images/loading.gif' />";
								TB_load_position();
								new Fx.Style(loadingid, 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
							 },
      						 onSuccess: function() {
							            	imManager.html = this.response['text'];
							            	$(loadingid).remove();
							            	$(moosmainid).setStyle('display', 'block');
							}}).send(tplfile, "");
	},
	
	/**
	 * get the right windoo theme according to the given theme name
	*/
	getTheme: function (themeName) {
		var otheme = null;
		switch (themeName) {
			case 'aero' :   otheme = Windoo.Themes.aero;
							break;
			case 'aqua' :  	otheme = Windoo.Themes.aqua;
							break;
			case 'alphacube':
			default :
							otheme = Windoo.Themes.alphacube;
							break;
		}
		return otheme;
	}
};
 